package com.demo.test.excel;

import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;

public class poiCreate1 {

	public void setBody(String[][] param, String[][] p) throws Exception {
		// 创建一个EXCEL
		Workbook workbook = new HSSFWorkbook();
		// 创建一个SHEET
		Sheet sheet = workbook.createSheet("example");

		Cell cell = null;
		Font font = null;

		CellStyle style = workbook.createCellStyle();
		style.setBorderTop(HSSFCellStyle.BORDER_THICK);// 上边框
		style.setBorderBottom(HSSFCellStyle.BORDER_THICK); // 下边框
		style.setBorderLeft(HSSFCellStyle.BORDER_THICK);// 左边框
		style.setBorderRight(HSSFCellStyle.BORDER_THICK);// 右边框

		style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中

		String[] head = { "基本资料", "职位", "认证", "工作经历" };
		int[] headWidth = { 0, 13, 15, 16, 19 };
		Row headRow = sheet.createRow((short) 0);
		int i = 0;
		for (String s : head) {
			CellStyle styleHead = workbook.createCellStyle();
			styleHead.cloneStyleFrom(style);
			styleHead.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);// 设置背景色
			font = workbook.createFont();
			font.setFontHeight(Font.BOLDWEIGHT_NORMAL);
			styleHead.setFont(font);
			cell = headRow.createCell(headWidth[i]);
			cell.setCellStyle(styleHead);
			cell.setCellValue(s);
			sheet.addMergedRegion(new CellRangeAddress(0, 0, headWidth[i],
					headWidth[i + 1] - 1));
			i++;
			cell = null;
			font = null;
		}
		i = 0;
		String[] title = { "用户名", "姓名", "性别", "专业组", "出生年月", "学历", "毕业学校",
				"通信地址", "公司", "电话", "邮箱", "身份证", "照片", "岗位", "技能等级", "认证",
				"时间", "就职单位", "岗位" };
		Row titleRow = sheet.createRow((short) 1);
		for (String s : title) {
			CellStyle styleTitle = workbook.createCellStyle();
			styleTitle.cloneStyleFrom(style);
			sheet.setColumnWidth(i, 5000);
			styleTitle.setFillForegroundColor(HSSFColor.BRIGHT_GREEN.index);// 设置背景色
			font = workbook.createFont();
			font.setFontHeight(Font.BOLDWEIGHT_NORMAL);
			styleTitle.setFont(font);
			cell = titleRow.createCell(i);
			cell.setCellStyle(styleTitle);
			cell.setCellValue(s);
			i++;
			cell = null;
			font = null;
		}
		int temp = 2;
		for (int l = 2; l < p.length + 2; l++) {
			int cha = temp + p.length - 1;
			Row bodyRow = sheet.createRow((short) l);
			System.out.println(bodyRow.getRowNum());
			for (int j = 0; j < title.length; j++) {
				if (j == 16) {
					cell = bodyRow.createCell(16);
					cell.setCellValue(p[l - 2][0]);
					cell = bodyRow.createCell(17);
					cell.setCellValue(p[l - 2][1]);
					cell = bodyRow.createCell(18);
					cell.setCellValue(p[l - 2][2]);
					break;
				}
				cell = bodyRow.createCell(j);
				cell.setCellValue("sss");
				sheet.addMergedRegion(new CellRangeAddress(temp, cha, j, j));
			}
			temp = cha + 1;
		}
		FileOutputStream fileOut = new FileOutputStream("d:\\test.xls");
		workbook.write(fileOut);
		fileOut.close();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		String[][] param = {
				{ "1", "2", "1", "2", "1", "2", "1", "2", "1", "2", "1", "2",
						"1", "2", "1", "2" },
				{ "1", "2", "1", "2", "1", "2", "1", "2", "1", "2", "1", "2",
						"1", "2", "1", "2" } };
		String[][] p = { { "爱爱爱", "嗖嗖嗖", "淡淡的" },
				{ "方芳芳", "广告歌", "去去去" }, { "呜呜呜", "嗯嗯额", "任溶溶" } };
		new poiCreate1().setBody(param, p);
	}
}